<?php

App::uses('AppController', 'Controller');
App::uses('Set', 'Util');

class ModelsController extends AppController {
    
    public $uses = array('BrandModel', 'Brand');
    
    public $title = 'Telefoni';
    
    public function index($brandId=null) {
        $conditions = array();
        if ($brandId) {
            $brand = $this->Brand->findById($brandId);
            $brand['Brand']['image'] = $this->getBrandImage($brand['Brand']['name']);
            $data  = $this->BrandModel->find('all', array(
                'fields'     => array('BrandModel.id', 'BrandModel.name'),
                'conditions' => $conditions = array('Brand.id' => $brandId)
            ));
            $view = 'models';
        } else {
            $brand = '';
            $data  = $this->Brand->find('list');
            $view  = 'brands';
        }
        
        $this->set(compact('data', 'brand'));
        $this->render($view);
    }
    
    public function search($keyword, $brandValue=null, $brandObject=null, $modelObject=null) {
        $this->layout = 'ajax';
        $func         = 'SelectModel';
        $object       = $brandObject . ";" . $modelObject;
        
        $data = $this->BrandModel->find('all', array(
            'fields' => array('BrandModel.id', 'BrandModel.name', 'Brand.name', 'Brand.id'),
            'conditions' => array(
                'BrandModel.name LIKE' => "%" . $keyword . "%"
            ),
            'order' => 'BrandModel.name',
            'limit' => 10
        ));
        if (! empty($data)) {
            $ddata = array();
            foreach ($data as $k=>$row) {
                $ddata[] = array(
                    'id'   => $row['BrandModel']['id'],
                    'name' => $row['Brand']['name'] . '/' . $row['BrandModel']['name'],
                    'data' => $row['Brand']['name'] . '{!}' . $row['BrandModel']['name']
                );
            }
            $data = $ddata;
        } else {
            header('Status:1');
        }
        
        $this->set(compact('data', 'func', 'object'));
        $this->render('/Elements/dropdown');
    }
    
    public function brand_create($name) {
        $exist = $this->Brand->findByName($name);
        if ($exist) {
            $this->flashError('Brend "'.$name.'" već postoji.');
        } else{
            $data = array('name'=>$name);
            if ($this->Brand->save($data)) {
                $this->flashOk('Brend "'.$name.'" je kreiran.');
            } else {
                $this->flashError('Brend "'.$name.'" nije kreiran.');
            }
        }
        return $this->redirect("index");
    }
    
    public function brand_delete($id) {
        $brandName = $this->Brand->field('name', array('id'=>$id));
        if ($this->Brand->delete($id)) {
            $brandImage = $this->getBrandImagePath($brandName);
            unlink($brandImage);
            $this->flashOk('Brend je obrisan.');
        } else {
            $this->flashError('Brend nije obrisan.');
        }
        $this->redirect('index');
    }
    
    protected function getBrandImage($name) {
        return Inflector::slug(strtolower($name), '_') . ".png";
    }
    protected function getBrandImagePath($name) {
        return WWW_ROOT . 'img' . DS . 'brands' . DS . $this->getBrandImage($name);
    }
    
    public function brand_image($brandId) {
        $file = $this->params['form']['file'];
        
        if ($file['error']) {
            $this->flashError('Odabrani fajl nije validan.');
        } else if(! preg_match('/\.png$/i', $file['name'])) {
            $this->flashError('Fajl nije PNG.');
        } else {
            $brandName = $this->Brand->field('name', array('Brand.id'=>$brandId));
            $dest = $this->getBrandImagePath($brandName);
            move_uploaded_file($file['tmp_name'], $dest);
        }
        $this->redirect('/models/index/' . $brandId);
    }
    
    public function create($brandId, $name) {
        $exist = $this->BrandModel->find('first', array(
            'conditions' => array(
                'BrandModel.brand_id' => $brandId,
                'BrandModel.name'     => $name
            )
        ));
        if ($exist) {
            $this->flashError('Model već postoji.');
        } else {
            $data = array(
                'brand_id' => $brandId,
                'name'     => $name
            );
            if($this->BrandModel->save($data)) {
                $this->flashOk('Model "'.$name.'" je kreiran.');
            } else {
                $this->flashError('Model "'.$name.'" nije kreiran.');
            }
        }
        
        return $this->redirect("index/{$brandId}");
    }
    
    public function delete($brandId, $modelId) {
        if ($this->BrandModel->delete($modelId)) {
            $this->flashOk('Model je obrisan.');
        } else {
            $this->flashError('Model nije obrisan');
        }
        $this->redirect('/models/index/' . $brandId);
    }
}

?>